package com.hdoit.ares.web.ser.providers;

import java.util.ArrayList;
import java.util.List;

import com.hdoit.ares.logical.ser.model.GoodStore;
import com.hdoit.ares.logical.ser.service.IGoodStoreService;
import com.hdoit.ares.logical.ser.service.IStaffService;
import com.hdoit.ares.logical.viewmodel.ser.GoodStoreSearch;
import com.tennetcn.common.aop.CommonApplicationContextUtil;
import com.tennetcn.common.enums.pub.YesOrNoInteger;
import com.tennetcn.common.logical.superbase.service.IRoleEmployeeService;
import com.tennetcn.common.message.ListItem;
import com.tennetcn.common.message.PagerModel;
import com.tennetcn.component.attribute.provider.ISingleOptionListProvider;
import com.tennetcn.component.web.SessionWebFactory;
import com.tennetcn.web.cbase.providers.base.DataListProviderBase;

public class StaffStoreProvider extends DataListProviderBase implements ISingleOptionListProvider<ListItem> {
	
	@Override
	public List<ListItem> getDataList(Object viewData) {
		List<GoodStore> goodStoreList=getGoodStoreList();
		if(goodStoreList==null||goodStoreList.size()<=0){
			return null;
		}
		dataList=new ArrayList<ListItem>();
		ListItem item;
		for (GoodStore goodStore:goodStoreList) {
			item=new ListItem();
			item.setText(goodStore.getStoreName());
			item.setValue(goodStore.getId());
			
			dataList.add(item);
		}
		return dataList;
	}
	
	private List<GoodStore> getGoodStoreList() {
		IGoodStoreService goodStoreService=CommonApplicationContextUtil.getCurrentContext().getBean(IGoodStoreService.class);
		IStaffService staffService=CommonApplicationContextUtil.getCurrentContext().getBean(IStaffService.class); 
		IRoleEmployeeService roleEmployeeService=CommonApplicationContextUtil.getCurrentContext().getBean(IRoleEmployeeService.class);
		
		SessionWebFactory sessionWebFactory=CommonApplicationContextUtil.getCurrentContext().getBean(SessionWebFactory.class);
		String loginId=sessionWebFactory.getCurrentLogin().getLoginId();
		
		boolean result=roleEmployeeService.queryCountRolesByEmployeeId("超级管理员",loginId);
		PagerModel pagerModel=new PagerModel();
		
		GoodStoreSearch goodStoreSearch=new GoodStoreSearch();
		goodStoreSearch.setDeleteMark(YesOrNoInteger.no);
		
		List<GoodStore> goodStoreList=null;
		if(goodStoreService.queryListStoreById(loginId).size()>0){
			goodStoreList=goodStoreService.queryListStoreById(loginId);
		}else{
			if(staffService.queryStaffById(loginId).size()>0){
			goodStoreList=goodStoreService.queryStoreById(loginId);
			}else if(result==true){
				goodStoreList=goodStoreService.queryAllStore(goodStoreSearch, pagerModel);
			}
		}

		return goodStoreList;
	}

}
